package com.xvyy.tingshu.user.strategy;

import com.xvyy.tingshu.common.execption.TsException;
import com.xvyy.tingshu.model.user.UserPaidAlbum;
import com.xvyy.tingshu.user.mapper.UserPaidAlbumMapper;
import com.xvyy.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ClassName: ProcessTypePayRecordByAlbum
 * Package: com.xvyy.tingshu.user.strategy
 *
 * @Description: 处理专辑类型的支付记录处理策略
 * @Author: xvyy
 * @Create: 2025/2/13 - 16:07
 * @Version: v1.0
 */
@Component(value = "1001")
@Slf4j
public class ProcessTypePayRecordByAlbum implements ProcessDefaultPayRecord {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public void processPayRecord(UserPaidRecordVo userPaidRecordVo) {
        String orderNo = userPaidRecordVo.getOrderNo();
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getOrderNo, orderNo));
        if (userPaidAlbum != null) {
            throw new TsException(201, "该专辑类型的订单已经处理过，请不要重复处理");
        }
        userPaidAlbum = new UserPaidAlbum();// 创建专辑支付记录对象
        userPaidAlbum.setOrderNo(orderNo);// 设置订单号
        userPaidAlbum.setUserId(userPaidRecordVo.getUserId());// 设置用户id
        userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));// 设置专辑id
        int count = userPaidAlbumMapper.insert(userPaidAlbum);
        log.info("保存订单号为{}的专辑类型{}，数据记录到数据库影响行数为{}", orderNo, count > 0 ? "成功" : "失败", count);
    }

}
